package com.jichaoyun.sys.controller;


import com.jichaoyun.common.enums.MyExceptiontType;
import com.jichaoyun.common.ex.MyException;
import com.jichaoyun.model.entity.StationInfo;

import com.jichaoyun.common.model.Result;
import com.jichaoyun.model.entity.RealdataCache;


import com.jichaoyun.sys.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@RestController
@Api(tags = {"单用户信息接口"})
@RequestMapping("/sys/singleUserInfo")
public class SingleUserController {

    @Autowired
    private IStationInfoService stationInfoService;

    @Autowired
    private IRealdataService realdataService;

    @Autowired
    private IFlowHourService flowHourService;

    @Autowired
    private IFlowDayService flowDayService;

    @Autowired
    private IFlowMonthService flowMonthService;

    @Autowired
    private IRealdataCacheService realdataCacheService;




    @ApiOperation("单用户信息，选择站点，查找该用户所有站点名称")
    @GetMapping("/getStationNameList")
    public Result<List<Map<String,Object>>> getStationList(@RequestParam(value = "id") Integer  id){
        return Result.success(stationInfoService.getStationNameList(id));
    }

    @ApiOperation("单用户信息，站点总量，站点余额，报警信息，查找当前站点的实时蒸汽量使用总量，用户余额，报警信息")
    @GetMapping("/getStationSingleInfoById")
    public Result<Map<String,Object>> getStationSingleInfoById(Integer station_no){
        HashMap<String, Object> map = new HashMap<>();
        RealdataCache data = realdataCacheService.getStationSingleInfoById(station_no);
        if(data == null){
            throw new MyException(MyExceptiontType.STATIONNODATA,null);
        }
        map.put("f_total",data.getTotal());
        map.put("money",data.getBalance());

        map.put("f_alarm",data.getFAlarm());
        map.put("pl_alarm",data.getPlAlarm());
        map.put("t_alarm",data.getTAlarm());
        map.put("v_alarm",data.getVAlarm());
        return Result.success(map);
    }

    @ApiOperation("智慧大屏，智慧大屏站点信息")
    @GetMapping("/getStationRealDataInfoById")
    public Result<RealdataCache> getStationRealDataInfoById(Integer station_no){

        RealdataCache data = realdataCacheService.getStationSingleInfoById(station_no);
        if(data == null){
            throw new MyException(MyExceptiontType.STATIONNODATA,null);
        }
        return Result.success(data);
    }

    @ApiOperation("单用户信息，实时流量，实时压力，实时温度，实时频率，获取该站点的实时流量,压力，温度，频率")
    @GetMapping("/getRealDataById")
    public Result<Map<String,Object>> getRealDataById(@RequestParam(value = "station_no") Integer station_no){
        HashMap<String, Object> data = new HashMap<>();

        ArrayList<String> times = new ArrayList<>();
        ArrayList<Float> flow = new ArrayList<>();
        ArrayList<Float> temp = new ArrayList<>();
        ArrayList<Float> pres = new ArrayList<>();
        ArrayList<Float> freq = new ArrayList<>();

        List<Map<String,Object>> datamap = realdataCacheService.getRealDataById(station_no);

        for (Map<String, Object> objectMap : datamap) {
            Timestamp t = (Timestamp) objectMap.get("record_time");
            Date date = t;
            // 将时间戳转换为Date对象
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            // 创建一个格式化对象，设置输出格式为时分
            String time = sdf.format(date);
            // 将Date对象格式化为时分字符串
            times.add(time);
            flow.add((Float) objectMap.get("flow"));
            freq.add((Float) objectMap.get("freq"));
            temp.add((Float) objectMap.get("temp"));
            pres.add((Float) objectMap.get("pres"));
        }
        data.put("timeList",times);
        data.put("flowList",flow);
        data.put("presList",pres);
        data.put("tempList",temp);
        data.put("freqList",freq);

        return Result.success(data);
    }


    @ApiOperation("单用户信息，实时流量top5")
    @GetMapping("/getTop5Flow")
    public Result<Map<String,Object>> getTop5Flow(Integer userId){
//        List<Realdata> realdata = realdataService.getTop5Flow();
        List<RealdataCache> realdata = realdataCacheService.getTop5Flow(userId);
        List<String> station_name = new ArrayList<>();
        List<Float> flow = new ArrayList<>();
        HashMap<String, Object> resMap = new HashMap<>();
        for (RealdataCache data : realdata) {
            station_name.add(data.getStationName());
            flow.add(data.getFlow());
        }
        resMap.put("station_name",station_name);
        resMap.put("flow",flow);
        return (Result.success(resMap));

    }

    @ApiOperation("单用户信息，实时温度top5")
    @GetMapping("/getTop5Temp")
    public Result<Map<String,Object>> getTop5Temp(Integer userId){
        List<RealdataCache> realdata = realdataCacheService.getTop5Temp(userId);
        List<String> station_name = new ArrayList<>();
        List<Float> temp = new ArrayList<>();
        HashMap<String, Object> resMap = new HashMap<>();
        for (RealdataCache data : realdata) {
            station_name.add(data.getStationName());
            temp.add(data.getTemp());
        }
        resMap.put("station_name",station_name);
        resMap.put("temp",temp);
        return (Result.success(resMap));

    }

    @ApiOperation("单用户信息，实时压力top5")
    @GetMapping("/getTop5Pres")
    public Result<Map<String,Object>> getTop5Pres(Integer userId){
        List<RealdataCache> realdata = realdataCacheService.getTop5Pres(userId);
        List<String> station_name = new ArrayList<>();
        List<Float> pres = new ArrayList<>();
        HashMap<String, Object> resMap = new HashMap<>();
        for (RealdataCache data : realdata) {
            station_name.add(data.getStationName());
            pres.add(data.getPres());
        }
        resMap.put("station_name",station_name);
        resMap.put("pres",pres);
        return (Result.success(resMap));
    }

    @ApiOperation(value = "单用户信息，单用户日报表，根据日期获取",notes = "date的格式为yyyy-MM-dd HH:mm:ss")
    @GetMapping("/getStationDayForm")
    public Result<Map<String,Object>> getStationDayForm(Integer stationNo, String date){

        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime end = LocalDateTime.parse(date, formatter);
        LocalDateTime start = end.minusHours(24);
        Map<String, Object> mapToday =  realdataService.get24HourNum(stationNo,start,end);
        if(mapToday==null){
           return Result.fail("存在异常数据，无法正确统计！！请管理员查询历史数据做人工校验");
        }

        return Result.success(mapToday);
    }

    @ApiOperation(value = "单用户信息，单用户日报表，导出到Excel")
    @GetMapping("/DownloadDaily")
    public void DownloadDaily(Integer stationNo,String date, HttpServletResponse response) throws IOException {

        flowHourService.getUserDayDataExcel(stationNo,date,response);

    }

    @ApiOperation(value = "单用户信息，单用户月报表，根据日期获取",notes = "时间传入年月就可以了")
    @GetMapping("/getStationMonthForm")
    public Result<List<Map<String,Object>>> getStationMonthForm(Integer station_no, Integer year,Integer month){
//        month = 2;

        StationInfo station = stationInfoService.getById(station_no);
        Float low_month = station.getDownFlow()* 24;
        Float up_month = station.getUpFlow()* 24;

        Map<String, Object> mapMonth =  flowDayService.getStationMonthForm(station_no, year,month);
        mapMonth.put("日下限",low_month);
        mapMonth.put("日上限",up_month);

        ArrayList<Map<String,Object>> resList = new ArrayList<>();
        resList.add(mapMonth);
        return Result.success(resList);
    }

    @ApiOperation(value = "单用户信息，单用户月报表，导出到Excel")
    @GetMapping("/DownloadMonth")
    public void DownloadMonth(Integer stationNo, Integer year, Integer month, HttpServletResponse response) throws IOException {

        flowDayService.getUserMonthDataExcel(stationNo,year,month,response);

    }


//    @ApiOperation("单用户信息，单用户年报表统计图")
//    @GetMapping("/getStationYearForm")
//    public Result<List<Map<String,Object>>> getStationYearForm(Integer Station_no, Integer year){
//
//        Map<String, Object> mapMonth =  flowMonthService.getStationYearForm(Station_no, year);
//        StationInfo station = stationInfoService.getById(Station_no);
//        Float low_month = station.getDownFlow()* 24* 30;
//        Float up_month = station.getUpFlow()* 24 * 30;
//        mapMonth.put("月下限",low_month);
//        mapMonth.put("月上限",up_month);
//        ArrayList<Map<String,Object>> resList = new ArrayList<>();
//        resList.add(mapMonth);
//
//        return Result.success(resList);
//
//    }



    @ApiOperation("单用户信息，单用户年报表统计图")
    @GetMapping("/getStationYearForm")
    public Result<List<Map<String,Object>>> getStationYearForm(Integer Station_no, String start,String end){

        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        LocalDateTime s = LocalDateTime.parse(start,formatter);
        LocalDateTime e = LocalDateTime.parse(end,formatter);

        Map<String, Object> mapMonth =  flowMonthService.getStationYearPeriodForm(Station_no,s,e);
        if(mapMonth == null) return Result.success();
        StationInfo station = stationInfoService.getById(Station_no);
        if(station.getDownFlow() == null){
            station.setDownFlow(0f);
        }
        if(station.getUpFlow() == null){
            station.setUpFlow(0f);
        }
        Float low_month = station.getDownFlow()* 24* 30;
        Float up_month = station.getUpFlow()* 24 * 30;
        mapMonth.put("月下限",low_month);
        mapMonth.put("月上限",up_month);
        ArrayList<Map<String,Object>> resList = new ArrayList<>();
        resList.add(mapMonth);

        return Result.success(resList);

    }


    @ApiOperation(value = "单用户信息，单用户年报表，导出到Excel")
    @GetMapping("/DownloadYear")
    public void DownloadYear(Integer stationNo, Integer year,HttpServletResponse response) throws IOException {

        flowMonthService.getUserYearDataExcel(stationNo,year,response);

    }
}
